﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Syngine.Components.Screens
{
	/// <summary>
	/// 
	/// </summary>
	public class ScreenController : Drawable
	{
		private readonly List<Screen> _screens = new List<Screen>();
		private readonly GameController _gameController;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="gameController"></param>
		public ScreenController(GameController gameController)
		{
			_gameController = gameController;
		}
		
		protected List<Screen> Screens
		{
			get
			{
				return _screens;
			}
		}

		protected Screen CurrentScreen { get; private set; }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="screen"></param>
		public virtual void Add(Screen screen)
		{
			if (screen != null)
			{
				if (!screen.IsInitialized)
				{
					screen.Initialize();
				}
				
				_screens.Add(screen);
			}
		}

		public override void Initialize()
		{
			for (int i = 0; i < _screens.Count; i++)
			{
				if (!_screens[i].IsInitialized)
				{
					_screens[i].Initialize();
				}
			}
		}

		public override void Update(UpdateContext context)
		{
			UpdateCore(context);
		}

		protected override void UpdateCore(UpdateContext context)
		{
			for (int i = 0; i < _screens.Count; i++)
			{
				_screens[i].Update(context);
			}
		}

		protected override void DrawCore(DrawContext context)
		{
			for (int i = 0; i < _screens.Count; i++)
			{
				_screens[i].Draw(context);
			}
		}

		public override void LoadContent(LoadContext context)
		{
			for (int i = 0; i < _screens.Count; i++)
			{
				_screens[i].LoadContent(context);
			}
		}

		public override void UnloadContent()
		{
			for (int i = 0; i < _screens.Count; i++)
			{
				_screens[i].UnloadContent();
			}
		}

		public void Show(Screen screen)
		{
			Show(screen, false);
		}

		public void Show(Screen screen, bool hideOtherScreens)
		{
			if (hideOtherScreens)
			{
				for (int i = 0; i < _screens.Count; i++)
				{
					_screens[i].Hide();
				}
			}

			screen.Visible = true;
			CurrentScreen = screen;
		}
	}
}